package com.imay.live.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.imay.base.fragment.BaseFragment;
import com.imay.base.util.ViewUtil;
import com.imay.live.activity.MainActivity2;
import com.imay.live.analytics.MobclickAgent;
import com.imay.live.model.NewMailModel;
import com.imay.live.utils.ToastUtil;
import com.imay.live.view.indicator.ScrollIndicatorView;
import com.imay.live.R;
import com.imay.live.adapter.IndicatorFragmentAdapter;
import com.imay.live.model.TabIndicatorModel;
import com.imay.live.view.indicator.IndicatorViewPager;
import com.imay.live.view.indicator.slidebar.ColorBar;
import com.imay.live.view.indicator.transition.OnTransitionTextListener;

import java.util.ArrayList;
import java.util.List;

/**
 * create by nick
 * 消息tab
 */
public class NoticeFragment extends BaseFragment{

    private ScrollIndicatorView view_Indicator;
    private ViewPager widget_viewpager;
    private List<TabIndicatorModel> mTabModelList;
    private IndicatorFragmentAdapter fragmentAdapter;
    private List<Fragment> fragmentList;
    private IndicatorViewPager indicatorViewPager;
    private RecentContactListFragment recentContactListFragment;
    private int currentIndex;

    public static NoticeFragment newInstance() {
        return new NoticeFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_notice;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        view_Indicator = $(R.id.notice_siv);
        widget_viewpager = $(R.id.notice_viewpager);
        initTitleBarRightView();
        bindFragment();

    }

    private void bindFragment() {

        recentContactListFragment = RecentContactListFragment.newInstance();
        mTabModelList = new ArrayList<>(3);
        mTabModelList.add(new TabIndicatorModel(getString(R.string.notice_tab_mine), false));
        mTabModelList.add(new TabIndicatorModel(getString(R.string.notice_tab_follow), false));
        mTabModelList.add(new TabIndicatorModel(getString(R.string.notice_tab_message), false));

        fragmentList = new ArrayList<>(3);
        fragmentList.add(NoticeChildFragment.newInstance(NoticeChildFragment.TYPE_MINE, 0));
        fragmentList.add(NoticeChildFragment.newInstance(NoticeChildFragment.TYPE_FOLLOW, 0));
        fragmentList.add(recentContactListFragment);
        widget_viewpager.setOffscreenPageLimit(2);

        ColorBar colorBar = new ColorBar(getActivity(), getActivity().getResources().getColor(R.color.indicator_bar_select), getActivity().getResources().getDimensionPixelOffset(R.dimen.indicator_height));
        colorBar.setWidth((int) ViewUtil.dip2px(getContext(), 25f));
        view_Indicator.setScrollBar(colorBar);


        view_Indicator.setOnTransitionListener(new OnTransitionTextListener().setColorId(getActivity(), R.color.indicator_text_select, R.color.indicator_text_unselect));
        indicatorViewPager = new IndicatorViewPager(view_Indicator, widget_viewpager);
        fragmentAdapter = new IndicatorFragmentAdapter(getActivity(), getChildFragmentManager(), fragmentList, mTabModelList) {
        };
        indicatorViewPager.setAdapter(fragmentAdapter);
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                currentIndex = currentItem;
                if (currentItem == 0) {
                    MobclickAgent.onEvent(getActivity(), "news_click_my");
                    MobclickAgent.onEvent(getActivity(), "news_my");
                } else if (currentItem == 1) {
                    MobclickAgent.onEvent(getActivity(), "news_click_attention");
                    MobclickAgent.onEvent(getActivity(), "news_attention");
                } else {
                    MobclickAgent.onEvent(getActivity(), "news_click_private");
                    MobclickAgent.onEvent(getActivity(), "news_private");
                    loadFamily();
                }
                showIgnoreUnreadBtn();

            }
        });
        widget_viewpager.setCurrentItem(1);
    }

    public void refreshRedPoint(boolean mineUnread, int index) {
        if (mTabModelList != null && mTabModelList.size() >= 3) {
            mTabModelList.get(index).setUnread(mineUnread);
            fragmentAdapter.getIndicatorAdapter().notifyDataSetChanged();
        }

    }

    public void showIgnoreUnreadBtn() {
        boolean visible = currentIndex == 2;
        MainActivity2 activity = (MainActivity2) getActivity();
        activity.getTitleBar().setRightViewVisibility(visible);
        activity.getTitleBar().setRightTextVisibility(visible);
    }

    private void initTitleBarRightView() {
        MainActivity2 activity = (MainActivity2) getActivity();
        activity.getTitleBar().addRightTextView(getString(R.string.system_notice_ignore_unread));
        activity.getTitleBar().setRightTextOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recentContactListFragment.hasUnread()) {
                    recentContactListFragment.ignoreRecentMessage();
                } else {
                    ToastUtil.show(getActivity(), R.string.system_notice_ignore_toast1);
                }
            }
        });
    }

    public int getRecentUnreadCount() {
        if (recentContactListFragment == null) {
            return 0;
        }
        return recentContactListFragment.getUnreadCount();

    }
    public void loadFamily() {
        if (recentContactListFragment != null) {
            recentContactListFragment.loadFamily();
        }

    }


    /**
     * 刷新系统通知
     */
    public void refreshSystemNotify() {
        if (recentContactListFragment != null) {
            recentContactListFragment.refreshSystemNotify();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (recentContactListFragment != null) {
            recentContactListFragment.onActivityResult(requestCode, resultCode, data);
        }
    }
}
